﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.ComponentModel;
using System.Diagnostics;
using BrainTechLLC.ThreadSafeObjects;
#if USE_HYPER
using Hyper.ComponentModel;
#endif

namespace BrainTechLLC.Logging
{
    /// <summary>
    /// Provides notification of new log entries - used to listen to a log in realtime
    /// </summary>    
    [Serializable]
    [DataContract]
    public class LogListener : ILogListener
    {
#if SILVERLIGHT
#else
        [field: NonSerialized]
#endif
        public event EventHandler<SimpleEventArgs<LogMessage>> NewLogEntry;

        public void OnNewLogEntry(SimpleEventArgs<LogMessage> eventArgs)
        {
            if (NewLogEntry != null)
            {
                NewLogEntry(this, eventArgs);
            }
        }

        public bool IsListening
        {
            get { return (NewLogEntry != null); }
            set
            {
                if (value == false)
                {
                    if (NewLogEntry != null)
                    {
                        NewLogEntry -= NewLogEntryReceived;
                    }
                }
                else
                {
                    if (NewLogEntry == null)
                    {
                        NewLogEntry += new EventHandler<SimpleEventArgs<LogMessage>>(NewLogEntryReceived);
                    }
                }
            }
        }

        public virtual void NewLogEntryReceived(object sender, SimpleEventArgs<LogMessage> args)
        {
            Debug.WriteLine(args.Value);
        }
    }
}
